
package com.rzico.core.plugin.market;

import com.rzico.core.entity.SysPlugin;
import com.rzico.core.entity.SysPluginAttribute;
import com.rzico.core.model.PluginAttribute;
import com.rzico.core.plugin.MarketPlugin;
import com.rzico.core.service.SysPluginAttributeService;
import com.rzico.util.CodeGenerator;
import com.rzico.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * Plugin -  京东实物对接API
 * 
 * @author RZICO.BOOT
 * @version 3.0
 */
@Component("marketJDPlugin")
public class MarketJDPlugin extends MarketPlugin {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	protected SysPluginAttributeService sysPluginAttributeService;

	@Override
	public String getName() {
		return "京东实物对接API";
	}

	@Override
	public String getVersion() {
		return "1.8.9";
	}

	@Override
	public List<PluginAttribute> getAttributeKeys() {
		List<PluginAttribute> data = new ArrayList<>();
//		data.add(new PluginAttribute("token","token",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("thumbnailPath","thumbnailPath",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("imgPath","imgPath",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("email","email",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("companyName","companyName",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("invoiceName","invoiceName",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("invoicePhone","invoicePhone",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("invoiceProvice","invoiceProvice",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("invoiceCity","invoiceCity",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("invoiceCounty","invoiceCounty",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("invoiceTown","invoiceTown",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("invoiceAddress","invoiceAddress",PluginAttribute.STRING_VALUE));
		data.add(new PluginAttribute("regCode","regCode",PluginAttribute.STRING_VALUE));
		return data;
	}

	/**
	 * 自动安装京东商品插件,并设置token属性
	 * 1.判断是否已安装京东商品插件，未安装的话自动安装插件及添加token属性值
	 * 2.已安装完京东商品插件，若未存在token属性值则添加，已存在token属性值则更新
	 * @param sysplugin
	 * @param token
	 * @param muchId
	 * @return
	 */
	public SysPlugin setToken(SysPlugin sysplugin, String token, String muchId){
		if (null == sysplugin){
			sysplugin = new SysPlugin();
			String sysPluginId = CodeGenerator.getUUID();
			sysplugin.setId(sysPluginId);
			sysplugin.setName("京东实物商品");
			sysplugin.setVersion("1.8.9.0");
			sysplugin.setPluginId("marketJDPlugin");
			sysplugin.setPluginType(5);
			sysplugin.setIsInstalled(false);
			sysplugin.setIsEnabled(false);
			sysplugin.setMchId(muchId);
			sysplugin.setCreateDate(new Date());
			sysPluginService.insert(sysplugin);
			SysPluginAttribute sysPluginAttribute = new SysPluginAttribute();
			sysPluginAttribute.setId(CodeGenerator.getUUID());
			sysPluginAttribute.setPluginId(sysPluginId);
			sysPluginAttribute.setName("token");
			sysPluginAttribute.setAttribute(token);
			sysPluginAttribute.setType("1");
			sysPluginAttributeService.insert(sysPluginAttribute);
			List<SysPluginAttribute> attributeList = new ArrayList<SysPluginAttribute>();
			attributeList.add(sysPluginAttribute);
			sysplugin.setSysPluginAttribute(attributeList);
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pluginId", sysplugin.getId());
			map.put("name", "token");
			List<SysPluginAttribute> sysPluginAttributeList = sysPluginAttributeService.selectList(map);
			/**
			 * 1.获取插件所有的属性
			 * 2.将所有的属性赋值给新的属性列表（除token外）
			 * 3.再将新的属性列表赋值给插件
			 */
			List<SysPluginAttribute> attributeList = sysplugin.getSysPluginAttribute();//获取插件的属性
			List<SysPluginAttribute> newAttributeList = new ArrayList<SysPluginAttribute>();//新的属性列表
			if (0 < attributeList.size()) {
				for (SysPluginAttribute attribute : attributeList) {
					if (!"token".equals(attribute.getName())) {
						newAttributeList.add(attribute);
					}
				}
			}
			SysPluginAttribute sysPluginAttribute = new SysPluginAttribute();
			if (0 < sysPluginAttributeList.size()) {
				//更新token属性
				sysPluginAttribute = sysPluginAttributeList.get(0);
				if (!token.equals(sysplugin.getAttribute("token"))) {
					sysPluginAttribute.setAttribute(token);
					sysPluginAttributeService.updateByPrimaryKey(sysPluginAttribute);
					newAttributeList.add(sysPluginAttribute);
					sysplugin.setSysPluginAttribute(newAttributeList);
				}
			} else {
				//添加token属性
				sysPluginAttribute.setId(CodeGenerator.getUUID());
				sysPluginAttribute.setPluginId(sysplugin.getId());
				sysPluginAttribute.setName("token");
				sysPluginAttribute.setAttribute(token);
				sysPluginAttribute.setType("1");
				sysPluginAttributeService.insert(sysPluginAttribute);
				newAttributeList.add(sysPluginAttribute);
				sysplugin.setSysPluginAttribute(newAttributeList);
			}
		}
		return sysplugin;
	}


	@Override
	public int compareTo(MarketPlugin o) {
		return 0;
	}
}